package com.community.controller;
import com.community.dao.ElderDao;
import com.community.dao.NoticeDao;
import com.community.dao.impl.ElderDaoImpl;
import com.community.dao.impl.NoticeDaoImpl;
import com.community.model.Elder;
import com.community.model.Notice;

import com.community.service.ElderService;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

@WebServlet("/ElderInsert")
public class ElderInsertController extends HttpServlet {
    private ElderService elderService =new ElderService();
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        int age = Integer.parseInt(request.getParameter("age"));
        String gender = request.getParameter("gender");
        String contact = request.getParameter("contact");
        String familyContact = request.getParameter("familyContact");
        String createdAtStr = request.getParameter("createdAt");
        LocalDateTime createdAt;
        if (createdAtStr != null && !createdAtStr.isEmpty()) {
            createdAt = LocalDateTime.parse(createdAtStr);
        } else {
            // 如果没有提供创建时间，使用当前时间
            createdAt = LocalDateTime.now();
        }
        Elder elder=new Elder();
         elder.setName(name);
         elder.setAge(age);
         elder.setGender(gender);
         elder.setContact(contact);
         elder.setFamilyContact(familyContact);
         elder.setCreatedAt(createdAt);
        // 调用service
         elderService.addElder(elder);
        response.sendRedirect(request.getContextPath() + "/ElderOverview"); // 发布成功后重定向到公告管理页面
    }
}